package com.baizhi.controller;

import com.baizhi.entity.AdminLog;
import com.baizhi.service.AdminLogService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("adminLog")
public class AdminLogController {

    @Autowired
    private AdminLogService adminLogService;

    @RequestMapping("getAll")
    @ResponseBody
    public Map getAdminLogs(@RequestParam(defaultValue = "1")Integer page,
                            @RequestParam(defaultValue = "10")Integer limt){
        HashMap map = new HashMap();
        try {
            PageInfo<AdminLog> pageInfo = adminLogService.findAll(page, limt);
            int count = (int) pageInfo.getTotal();
            List<AdminLog> logs = pageInfo.getList();
            map.put("count", count);
            map.put("data", logs);
            map.put("code", 0);
            return map;
        } catch (RuntimeException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    @RequestMapping("delBulk")
    @ResponseBody
    public Boolean deleteAdminLogs(Integer[] ids){
        try {
            adminLogService.removeAdminLogMultiple(ids);
            return true;
        } catch (RuntimeException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    @RequestMapping("delById")
    @ResponseBody
    public Boolean deleteAdminLogById(AdminLog adminLog){
        try {
            adminLogService.removeAdminLogById(adminLog.getLogId());
            return true;
        } catch (RuntimeException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

}
